RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

# symbol link to home
function _stoh() {

    if [ -L $2 ] ; then
	if [ $(readlink -f $2) == $1 ]; then
	    echo -e "$2 ${GREEN}already exists${NC}"
	    return 1
	else
	    _backup $2
	fi
    fi

    if [ -e $2 ] ; then
	    _backup $2
    fi

    ln -s $1 $2
    echo -e "link $1 -> $2 ${GREEN}ok${NC}"
}

function _backup() {
    if [[ ! -d $HOME/dotfiles/.backup ]]; then
	    mkdir -p $HOME/dotfiles/.backup
    fi

    newpath=$HOME/dotfiles/.backup/$(basename $1)-$(date +%s)
        echo "backup $1 to ${newpath}"
    mv $1 ${newpath}
}

# bootstrap config files
function bootstrap() {
    _stoh "$HOME/dotfiles/.config/alacritty"           "$HOME/.config/alacritty"
    _stoh "$HOME/dotfiles/.config/qtile"               "$HOME/.config/qtile"
    _stoh "$HOME/dotfiles/.config/picom"               "$HOME/.config/picom"
    _stoh "$HOME/dotfiles/.config/bash/.bash_profile"  "$HOME/.bash_profile"
    _stoh "$HOME/dotfiles/.config/bash/.bashrc"        "$HOME/.bashrc"
    _stoh "$HOME/dotfiles/.config/vim/small.vimrc"     "$HOME/.vimrc"
    _stoh "$HOME/dotfiles/.config/x11/.Xresources"     "$HOME/.Xresources"
    _stoh "$HOME/dotfiles/.config/x11/.xinitrc"        "$HOME/.xinitrc"
    _stoh "$HOME/dotfiles/.config/zsh/.zshrc"          "$HOME/.zshrc"
}

"$@"
